package com.sishuok.jmstest.topic;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;

public class PersistenceReceiver {
	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.15.111:61616");
		Connection connection = connectionFactory.createConnection();
		connection.setClientID("cc1");
		
		Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		Topic destination = session.createTopic("VirtualTopic.Mirror.my-queue");//("my-topic");
//		MessageConsumer consumer = session.createConsumer(destination);
		TopicSubscriber ts = session.createDurableSubscriber(destination, "T1");
		connection.start();
		
		Message message = ts.receive();
		while(message != null){
			TextMessage txtMsg = (TextMessage) message;
			session.commit();
			System.out.println("收到消息："+txtMsg.getText());
			message = ts.receive(1000L);
		}
		session.close();
		connection.close();
	}
}
